#!D:/CODE/python
# -*- coding: utf-8 -*-
# @Time : 2020/5/24 18:26
# @Author : Alexdong
# @Site : 
# @File : 进程池.py
# @Software: PyCharm
# Functional description:

from multiprocessing import Pool
import os, time, random

def worker(msg):
    t_start = time.time()
    print("%s 开始执行进程号：%d" % (msg, os.getpid()))
    time.sleep(random.random()*2)
    t_stop = time.time()
    print(msg, "执行完毕，耗时%0.2f " % (t_stop - t_start))

def main():
    po = Pool(3)
    for i in range(0, 10):
        # Pool.apply_async(要调用的目标，（传递给目标的参数元组，））
        # 每次循环将会用空闲出来的子进程去调用目标
        po.apply_async(worker, (i,))

    print("----start----")
    po.close()
    po.join()  # 等待po中所有子进程执行完成，必须放在close语句之后
    print("----end------")

if __name__ =="__main__":
    main()